package com.cn.web;

import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;

/**
 * Created by chenyuncong on 2019/5/6.
 */
@Controller
@RequestMapping("/servers")
public class TestWeb {

    @RequestMapping(value = "/zk",headers="Accept=application/x-www-form-urlencoded")
    public void zk(HttpServletResponse response) throws  Exception{
        FormHttpMessageConverter converter = new FormHttpMessageConverter();
        converter.setCharset(Charset.forName("utf-8"));
        HttpOutputMessage output = new ServletServerHttpResponse(response);
        converter.write(buildServers(), MediaType.APPLICATION_FORM_URLENCODED,output);
        //String servers = "count=2&port=2181&server0=127.0.0.1&server1=localhost";
    }


    private MultiValueMap<String,Object> buildServers(){
        MultiValueMap<String,Object> map = new LinkedMultiValueMap<String, Object>();
        map.add("count","2");
        map.add("port","2181");
        map.add("server0","127.0.0.1");
        map.add("server1","localhost");
        return map;
    }
}
